﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xiami.Helper.Model;
using AMing.Extension.Net40;

namespace Html5Music.Controllers
{
    public class HomeController : Controller
    {
        #region Data
        private Data.PlayListData _playlist = null;

        public Data.PlayListData Playlist
        {
            get
            {
                if (_playlist == null)
                {
                    Data.Config.Path_PlayList = Server.MapPath("~/file/playlist.txt");
                    _playlist = new Data.PlayListData();
                }
                return _playlist;
            }
        }
        private Data.UserListData _userlist = null;

        public Data.UserListData Userlist
        {
            get
            {
                if (_userlist == null)
                {
                    Data.Config.Path_UserList = Server.MapPath("~/file/userlist.txt");
                    _userlist = new Data.UserListData();
                }
                return _userlist;
            }
        }
        #endregion
        //
        // GET: /Home/

        public ActionResult Index()
        {
            //var user = Userlist.Login("admin", "pwd");

            //Playlist.Add(new Model.PlayList
            //{
            //    UserID = user.ID,
            //    Share = true,
            //    XiamiPageUrl = "http://www.xiami.com/album/1386742453",
            //    XiamiPlayUrl = "http://www.xiami.com/song/play?ids=/song/playlist/id/1386742453/type/1",
            //    Key = "keytest"
            //});

            return View();
        }

        public ActionResult Player(string url, string key, Guid? guid)
        {
            string playurl = url;
            Model.PlayList playlist = null;
            if (!string.IsNullOrWhiteSpace(key))
            {
                playlist = Playlist.GetByKey(key);
            }
            if (guid.HasValue)
            {
                playlist = Playlist.GetByID(guid.Value);
            }
            if (playlist != null && playlist.Share)
            {
                playurl = playlist.XiamiPlayUrl;
                ViewBag.Des = "<br/><a href='{0}' target='_blank'>{0}</a>".ToFromat(playlist.XiamiPageUrl);
            }
            ViewBag.PlayListUrl = playurl;

            return View();
        }

        public ActionResult PlayList(string url)
        {
            if (string.IsNullOrWhiteSpace(url))
            {
                return Json("url is null.", JsonRequestBehavior.AllowGet);
            }
            url = System.Web.HttpUtility.UrlDecode(url);
            Xiami.Helper.Decode xiamiHelper = new Xiami.Helper.Decode();
            var list = xiamiHelper.GetTrackData(url);
            var music_list = GetList(list);

            return Json(music_list, JsonRequestBehavior.AllowGet);
        }


        private object GetList(TrackListInfo xiamiList)
        {
            List<Model.music> list = new List<Model.music>();
            foreach (var item in xiamiList.TrackList)
            {
                var music = new Model.music()
                {
                    artist = item.artist,
                    mp3 = item.MP3Url,
                    cover = item.Pic_2,
                    title = item.Title,
                    rating = 5
                };
                list.Add(music);
            }

            return list;
        }
    }
}
